package com.cmict.data.infra.controller.admin.consulthistory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.common.pojo.CommonResult;
import static com.cmict.data.framework.common.pojo.CommonResult.success;

import com.cmict.data.framework.excel.core.util.ExcelUtils;

import com.cmict.data.framework.operatelog.core.annotations.OperateLog;
import static com.cmict.data.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cmict.data.infra.controller.admin.consulthistory.vo.*;
import com.cmict.data.infra.dal.dataobject.consulthistory.ConsultHistoryDO;
import com.cmict.data.infra.convert.consulthistory.ConsultHistoryConvert;
import com.cmict.data.infra.service.consulthistory.ConsultHistoryService;

@Api(tags = "管理后台 - 咨询历史管理")
@RestController
@RequestMapping("/infra/consult-history")
@Validated
public class ConsultHistoryController {

    @Resource
    private ConsultHistoryService consultHistoryService;

    @PostMapping("/create")
    @ApiOperation("创建咨询历史管理")
    @PreAuthorize("@ss.hasPermission('sys:consult-history:create')")
    public CommonResult<Long> createConsultHistory(@Valid @RequestBody ConsultHistoryCreateReqVO createReqVO) {
        return success(consultHistoryService.createConsultHistory(createReqVO));
    }

    @PostMapping("/robotChat")
    @ApiOperation("机器聊天 ")
    public CommonResult<ConsultHistoryDO> createHistoryByRobotChat(@Valid @RequestBody ConsultHistoryCreateReqVO createReqVO) {
        return success(consultHistoryService.createHistoryByRobotChat(createReqVO));
    }

    @PostMapping("/chatHistory")
    @ApiOperation("获得聊天记录")
    public CommonResult<List<ConsultHistoryRespVO>> getChatHistoryList(@RequestBody ConsultHistoryCreateReqVO createReqVO) {
        List<ConsultHistoryDO> list = consultHistoryService.getChatHistoryList(createReqVO);
        return success(ConsultHistoryConvert.INSTANCE.convertList(list));
    }

    @PutMapping("/update")
    @ApiOperation("更新咨询历史管理")
    @PreAuthorize("@ss.hasPermission('sys:consult-history:update')")
    public CommonResult<Boolean> updateConsultHistory(@Valid @RequestBody ConsultHistoryUpdateReqVO updateReqVO) {
        consultHistoryService.updateConsultHistory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除咨询历史管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:consult-history:delete')")
    public CommonResult<Boolean> deleteConsultHistory(@RequestParam("id") Long id) {
        consultHistoryService.deleteConsultHistory(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得咨询历史管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:consult-history:query')")
    public CommonResult<ConsultHistoryRespVO> getConsultHistory(@RequestParam("id") Long id) {
        ConsultHistoryDO consultHistory = consultHistoryService.getConsultHistory(id);
        return success(ConsultHistoryConvert.INSTANCE.convert(consultHistory));
    }

    @GetMapping("/list")
    @ApiOperation("获得咨询历史管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('sys:consult-history:query')")
    public CommonResult<List<ConsultHistoryRespVO>> getConsultHistoryList(@RequestParam("ids") Collection<Long> ids) {
        List<ConsultHistoryDO> list = consultHistoryService.getConsultHistoryList(ids);
        return success(ConsultHistoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得咨询历史管理分页")
    @PreAuthorize("@ss.hasPermission('sys:consult-history:query')")
    public CommonResult<PageResult<ConsultHistoryRespVO>> getConsultHistoryPage(@Valid ConsultHistoryPageReqVO pageVO) {
        PageResult<ConsultHistoryDO> pageResult = consultHistoryService.getConsultHistoryPage(pageVO);
        return success(ConsultHistoryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出咨询历史管理 Excel")
    @PreAuthorize("@ss.hasPermission('sys:consult-history:export')")
    @OperateLog(type = EXPORT)
    public void exportConsultHistoryExcel(@Valid ConsultHistoryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ConsultHistoryDO> list = consultHistoryService.getConsultHistoryList(exportReqVO);
        // 导出 Excel
        List<ConsultHistoryExcelVO> datas = ConsultHistoryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "咨询历史管理.xls", "数据", ConsultHistoryExcelVO.class, datas);
    }

}
